<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<script src="../static/plugins/layui/layui.js"></script>
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">

<!-- 内容主体区域 -->
<div class="layui-row layui-pad15" ng-app="addUniterm" ng-controller="addUnitermController">
        <form class="layui-form" action="">
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row">
                     <div class="layui-col-xs2">单项名称</div>
                    <div class="layui-col-xs9">
                        <input type="text" name="title"  autocomplete="off" class="form-control" id="unitermName" ng-model="iterm.name" value="{{iterm.itermName}}">
                    </div>
                </div>
            </div>
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row">
                    <div class="layui-col-xs2">单项价格</div>
                    <div class="layui-col-xs9">
                        <div class="input-group">
                            <div class="input-group-addon">¥</div>
                            <input type="text" name="title"  autocomplete="off" class="form-control" id="price" ng-model="iterm.price" value="{{iterm.price}}">
                            <div class="input-group-addon">元</div>
                        </div>
                    </div>
                    <!--<div class="layui-col-xs1">元</div>-->
                </div>
            </div>
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row">
                    <div class="layui-col-xs2">单项代码</div>
                    <div class="layui-col-xs9">
                        <input type="text" name="title"  autocomplete="off" class="form-control" id="code" ng-model="iterm.code" value="{{iterm.code}}">
                    </div>
                </div>
            </div>
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row">
                    <div class="layui-col-xs2">单项说明</div>
                    <div class="layui-col-xs9">
                        <textarea  class="form-control" rows="3" id="brief" ng-model="iterm.brief">{{iterm.brief}}</textarea>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12  layui-text-center layui-col-lh">
                <!--<a href="#" class="layui-q-c layui-t-can layui-dels ayui-btn layui-btn-primary" ng-click="closeIframe()">取消</a>-->
                <a href="#" class="layui-q-c layui-zdy-bj" ng-click="add_uniterm()">
                    确定
                </a>
            </div>
    </form>


</div>
<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>

    function alertMsg(msg) {
        layui.use(['layer'], function () {
            var layer = layui.layer;
            layer.msg(msg);
        });
    }

    var getLocalUnitermURL = baseUrl + "/exam/getLocalUniterm";
    var addLocalUnitermURL = baseUrl + "/exam/addLocalUniterm";
    var updateLocalUnitermURL = baseUrl + "/exam/updateLocalUniterm";

	var app = angular.module("addUniterm",[]);
	app.controller('addUnitermController', function($scope, $http) {

	    $scope.closeIframe = function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        }

	    var localUnitermId = "";

	    $(function () {

            $scope.iterm = {
                "name":"",
                "code":"",
                "price":"",
                "brief":""
            };

            var url = location.search;
            console.log(url);
            var unitermId = "";
            if(url.indexOf("?") != -1) {
                var str = url.substr(1);
                unitermId = str.split("=")[1];
            }
            console.log(unitermId);
            localUnitermId = unitermId;
            if(unitermId) {
                $http({
                    method:"GET",
                    url:getLocalUnitermURL,
                    params:{
                        "token":"123456",
                        "localUnitermId":unitermId
                    }
                }).then(function successCallback(result) {
                    console.log(result.data.data);
                    $scope.uniterm = result.data.data;
                    $scope.iterm.name = $scope.uniterm.name;
                    $scope.iterm.code = $scope.uniterm.code;
                    $scope.iterm.brief = $scope.uniterm.brief;
                    $scope.iterm.price = $scope.uniterm.price/100 + "." + $scope.uniterm.price%100;
                });
            }
        });

        $scope.itermName = "";
        $scope.code = "";
        $scope.brief = "";
        $scope.price = "";
        //创建单项
		$scope.add_uniterm = function(){
		    var price = $("#price").val()?($("#price").val()):""
            if(isNaN(price)) {
                alertMsg("金额请输入数字");
                return
            }

            var data = {
                "token": "22222",
                "localUnitermName":$("#unitermName").val()?($("#unitermName").val()):"",
                "code":$("#code").val()?($("#code").val()):"",
                "brief":$("#brief").val()?($("#brief").val()):"",
                "price":price
            };
            console.log(data);
            if(localUnitermId === "" || localUnitermId === "0") {
                //执行添加操作
                $http({
                    method:"POST",
                    url:addLocalUnitermURL,
                    data:data,
                    headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                    transformRequest: function(obj) {
                        var str = [];
                        for(var p in obj){
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    }
                }).then(
                    function successCallback(result){
                        if (parseInt(result.data.state) === 1){
                            console.log(result.data.message);
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }
                    },
                    function errorCallback(result) {
                        console.log("error");
                        console.log(result);
                    }
                );
            } else {
                //执行更新操作
                data.localUnitermId = localUnitermId;
                $http({
                    method:"POST",
                    url:updateLocalUnitermURL,
                    data:data,
                    headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                    transformRequest: function(obj) {
                        var str = [];
                        for(var p in obj){
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    }
                }).then(
                    function successCallback(result){
                        if (parseInt(result.data.state) == 1){
                            console.log(result.data.message);
                            window.parent.location.reload();//刷新父页面
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }
                    },
                    function errorCallback(result) {
                        console.log("error");
                        console.log(result);
                    }
                );
            }
		}
	});
	
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
</script>


